home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Special 25 / AMIGAplus Sonderheft 25 (2000)(Falke)(DE)(Track 1 of 4)[!].iso / Updates / Hardware / FreeWheel / Icon.c < prev    next >
C/C++ Source or Header  |  2000-03-12  |  4KB  |  155 lines

  1. /****************************************************************************
  2.  
  3.   Support routines for dealing with a program's icon.
  4.   Simplifies the handling  of Tooltypes, and generally helps
  5.   keep things tidy.
  6.  
  7.   © 1998 by Alastair M. Robinson.
  8.  
  9.   You may use the routines in this file as you like.
  10.  
  11. ****************************************************************************/
  12.  
  13. #include <exec/types.h>
  14. #include <workbench/icon.h>
  15. #include <workbench/startup.h>
  16. #include <dos/dos.h>
  17. #include <clib/exec_protos.h>
  18. #include <clib/dos_protos.h>
  19. #include <clib/icon_protos.h>
  20.  
  21. #include "Icon.h"
  22.  
  23.         /* I hope you won't need this global variable. */
  24.  
  25. struct DiskObject *MyDiskObject;
  26.  
  27.  
  28.         /* The C Startup code usually defines the Workbench Startup Messge,
  29.            though your compiler might call it something other than WBenchMsg */
  30.  
  31. extern struct WBStartup *WBenchMsg;
  32.  
  33.  
  34. /****************************************************************************
  35.   This is just a stub to the icon.library MatchToolValue function.
  36.   Use it like this:
  37.  
  38.   if(MatchToolType("CX_POPUP","YES"))
  39.     OpenMyGUI();
  40.  
  41. ****************************************************************************/
  42.  
  43. BOOL MatchToolType(char *tooltype,char *value)
  44. {
  45.   char *val2;
  46.   if(val2=GetStringToolType(tooltype))
  47.   {
  48.     return(MatchToolValue(val2,value));
  49.   }
  50.   return(FALSE);
  51. }
  52.  
  53.  
  54. /****************************************************************************
  55.   Another stub routine, written partly to accompany GetNumericToolType, but
  56.   mostly so that external modules don't have to know about the MyDiskObject
  57.   global variable.  It just tidies things up, and makes it easier to re-use
  58.   the code.  Example usage:
  59.  
  60.   printf("ToolType Channel0 is set to %s\n",GetStringToolType("Channel0"));
  61.  
  62. ****************************************************************************/
  63.  
  64. char *GetStringToolType(char *tooltype)
  65. {
  66.   return(FindToolType((unsigned char**)MyDiskObject->do_ToolTypes,tooltype));
  67. }
  68.  
  69.  
  70. /****************************************************************************
  71.   Cute little support routine to translate from ASCII to integer.  Called
  72.   by the GetNumericToolType() routine.  This is used instead of sscanf()
  73.   simply to avoid the need to link with the standard C library, thus reducing
  74.   the size of the final executable.  The second parameter, defvalue is the
  75.   number which will be returned if the string pointer turns out not to point
  76.   to a valid number!
  77. ****************************************************************************/
  78.  
  79. int Ascii2Num(char *str,int defvalue)
  80. {
  81.   char c;
  82.   int v=0;
  83.   while(c=*str++)
  84.   {
  85.     if((c<48)||(c>57))
  86.       return(defvalue);
  87.     v*=10; v+=c-48;
  88.   }
  89.   return(v);
  90. }
  91.  
  92.  
  93. /****************************************************************************
  94.   This nice little routine hunts out a tooltype, and converts it from ASCII
  95.   to an integer, returning defvalue if anything goes wrong.
  96.   Example:
  97.  
  98.   mc->Left=GetNumericToolType("Left0",63);
  99.  
  100. ****************************************************************************/
  101.  
  102. int GetNumericToolType(char *tooltype,int defvalue)
  103. {
  104.   char *value;
  105.   int result;
  106.   if(value=FindToolType((unsigned char**)MyDiskObject->do_ToolTypes,tooltype))
  107.   {
  108.     defvalue=Ascii2Num(value,defvalue);
  109.   }
  110.   return(defvalue);
  111. }
  112.  
  113.  
  114. /****************************************************************************
  115.   This routine just grabs the DiskObject associated with the program.
  116.   This is actually harder than you might think  -  the current directory
  117.   might not necessarily be the directory which contains the program and its
  118.   icon, though this can probably be assumed when running from Workbench.
  119. ****************************************************************************/
  120.  
  121. BOOL Icon_Setup()
  122. {
  123.   char name[64];
  124.  
  125.   if(WBenchMsg)
  126.   {
  127.     struct WBArg *wb=WBenchMsg->sm_ArgList;
  128.     if(!(MyDiskObject=GetDiskObject(wb->wa_Name)))
  129.       return(FALSE);
  130.   }
  131.   else
  132.   {
  133.     BPTR oldlock;
  134.     oldlock=CurrentDir(GetProgramDir());
  135.     GetProgramName(name,64);
  136.     MyDiskObject=GetDiskObject(name);
  137.     CurrentDir(oldlock);
  138.     if(!MyDiskObject)
  139.       return(FALSE);
  140.   }
  141.   return(TRUE);
  142. }
  143.  
  144.  
  145. /****************************************************************************
  146.   Much simpler routine to free everything (!) allocated by Icon_Setup().
  147. ****************************************************************************/
  148.  
  149. void Icon_Cleanup()
  150. {
  151.   if(MyDiskObject)
  152.     FreeDiskObject(MyDiskObject);
  153. }
  154.  
  155.